package cn.dliyy.warehouse.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.dliyy.warehouse.bean.BUnit;
import cn.dliyy.warehouse.service.BUnitService;

@Controller
@RequestMapping(value = "/unit")
public class UnitController {

	@Resource
	private BUnitService unitService;
	@Resource
	private BUnit unit;

	public UnitController() {
	}

	@RequestMapping(value = "/")
	public String load(ModelMap modelMap) {
		List<Object> list = unitService.getUnitList();
		modelMap.put("list", list);
		return "/base/unit";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		modelMap.addAttribute("unit", unit);
		return "/base/unit_add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView create(@Valid BUnit unit, BindingResult result) {
		ModelAndView mav = new ModelAndView();
		if (result.hasErrors()) {
			mav.setViewName("/base/unit_add");
			return mav;
		}
		unitService.save(unit);
		mav.setViewName("redirect:/unit/");
		return mav;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String load(ModelMap modelMap, String t) {
		List<Object> list = unitService.getUnitList();
		modelMap.put("list", list);
		return "/base/box/unitlist";
	}
}
